import operate from '@dfeidao/atom-nodejs/db/operate';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	hidden_source: string;
	filter: {
		duty_department_no: string;
		duty_department: string;
		responsible: string;
		responsible_task_no: string;
		hidden_code: string;
		hidden_name: string;
		control_level: string;
		control_grade: string;
		assess_level: string;
		engineer_skill: string;
		manage_measure: string;
		train_education: string;
		emergency_measures: string;
		personal_protect: string;
	};
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
/* 表名	hazard_source
标题	隐患源
字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
_id	id	string	50	ⅹ	√
productid	产品ID	string	50	√	ⅹ
hidden_source	隐患源编号	string	32	√	ⅹ
duty_department_no	责任部门编号	string	50	√	ⅹ
duty_department	责任部门名称	string	50	√	ⅹ
responsible	责任人名称	string	50	√	ⅹ
responsible_task_no	任务责任人编号	string	50	√	ⅹ
hidden_code	隐患源代码	string	32	√	ⅹ
hidden_name	隐患源名称	string	32	√	ⅹ
control_level	管控级别	string	32	√	ⅹ
control_grade	管控层级	string	32	√	ⅹ
assess_level	评价级别	string	32	√	ⅹ
engineer_skill	工程技术	string	128	√	ⅹ
manage_measure	管理措施	string	128	√	ⅹ
train_education	培训教育	string	128	√	ⅹ
emergency_measures	应急处置措施	string	128	√	ⅹ
personal_protect	个体防护	string	128	√	ⅹ
 */
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf-hManager\zj-000005\s002,action_id:' + action_id);

	const { update, exec } = operate(action_id, session_id, systemid);
	// 保存修改后的数据
	update('hazard_source', { productid, hidden_source: message.hidden_source }, {
		duty_department_no: message.filter.duty_department_no,
		duty_department: message.filter.duty_department,
		responsible: message.filter.responsible,
		responsible_task_no: message.filter.responsible_task_no,
		hidden_code: message.filter.hidden_code,
		hidden_name: message.filter.hidden_name,
		control_level: message.filter.control_level,
		control_grade: message.filter.control_grade,
		assess_level: message.filter.assess_level,
		engineer_skill: message.filter.engineer_skill,
		manage_measure: message.filter.manage_measure,
		train_education: message.filter.train_education,
		emergency_measures: message.filter.emergency_measures,
		personal_protect: message.filter.personal_protect,
		productid
	});

	const r = await exec();

	log('Service end path:f:\实习\kh\src\wyf-hManager\zj-000005\s002,action_id:' + action_id);
	return {
		data: r
	};
}
